﻿using ProjBackEnd.Areas.BizPurview.Models;
using ProjBackEnd.DBInstance;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;

namespace ProjBackEnd.DAL
{
    public class bizPurviewLogic
    {

        public static RespProjPurview GetProjPurviewMember(int _iProjid)
        {
            RespProjPurview respReturn = new RespProjPurview();
            BizPurviewDataContext bpctx = new BizPurviewDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<ProjPurviewExtInfo> lst = bpctx.VIEW_PUR_PROJPURVIEW.Where(n => n.projid == _iProjid).AsEnumerable()
                                                                                                                .Select(n => new ProjPurviewExtInfo
                                                                                                                {
                                                                                                                    memberid = n.memberid,
                                                                                                                    membername = n.username,
                                                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                                    projid = n.projid
                                                                                                                }).ToList();
                respReturn.result = new ResultProjPurview { purviewlist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }


        public static RespPurNullResultInfo UpdateProjPurview(string _strMemberid, ProjPurviewBaseInfo _collec)
        {
            RespPurNullResultInfo respReturn = new RespPurNullResultInfo();
            BizPurviewDataContext bpctx = new BizPurviewDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                int iPurCount = bpctx.PUR_MANAGERTB.Count(n => n.purtp == GlobalSettings.PURTP_PROJ && n.memberid == _strMemberid);
                if (iPurCount == 0) throw new BizException("无操作权限");
                switch (_collec.optlb)
                {
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        PUR_PROJ entityIns = new PUR_PROJ { memberid = _collec.memberid, projid = _collec.projid };
                        bpctx.PUR_PROJ.InsertOnSubmit(entityIns);
                        break;
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        PUR_PROJ entityDel = bpctx.PUR_PROJ.SingleOrDefault(n => n.projid == _collec.projid && n.memberid == _collec.memberid);
                        if (entityDel == default(PUR_PROJ)) throw new BizException("未找到项目成员信息");
                        bpctx.PUR_PROJ.DeleteOnSubmit(entityDel);
                        break;
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }


        public static RespPurviewMgr GetPurviewMgr(string _strComcd, string _strPurtp)
        {
            RespPurviewMgr respReturn = new RespPurviewMgr();
            BizPurviewDataContext bpctx = new BizPurviewDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<PurviewMgrExtInfo> lst = bpctx.VIEW_PUR_MANAGERINFO.Where(n => n.purtp == _strPurtp && n.comcd == _strComcd).AsEnumerable()
                                                                                                                    .Select(n => new PurviewMgrExtInfo
                                                                                                                    {
                                                                                                                        memberid = n.memberid,
                                                                                                                        mgrnm = n.mgrnm,
                                                                                                                        optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                                        purtp = n.purtp,
                                                                                                                        purtpnm = n.purtpnm
                                                                                                                    }).ToList();
                respReturn.result = new ResultPurviewMgr { purmgrlist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

        public static RespPurNullResultInfo UpdatePurviewMgr(string _strMember,string _strComcd,PurviewMgrCore _collec)
        {
            RespPurNullResultInfo respReturn = new RespPurNullResultInfo();
            BizPurviewDataContext bpctx = new BizPurviewDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                int iPurtpCount = bpctx.CD_BASIC_purview.Count(n => n.attrcd == "PURTP" && n.cd == _collec.purtp);
                if(iPurtpCount == 0) throw new BizException("未找到权限管理类别");
                switch (_collec.optlb)
                {
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        PUR_MANAGERTB entityIns = new PUR_MANAGERTB
                        {
                            comcd = _strComcd,
                            createdt = DateTime.Now,
                            createid = _strMember,
                            memberid = _collec.memberid,
                            purtp = _collec.purtp
                        };
                        bpctx.PUR_MANAGERTB.InsertOnSubmit(entityIns);
                        break;
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        PUR_MANAGERTB entityDel = bpctx.PUR_MANAGERTB.SingleOrDefault(n => n.purtp == _collec.purtp && n.memberid == _collec.memberid);
                        if (entityDel == default(PUR_MANAGERTB)) throw new BizException("未找到权限管理员信息");
                        bpctx.PUR_MANAGERTB.DeleteOnSubmit(entityDel);
                        break;
                    default: throw new Exception("操作标识符异常");
                }
                bpctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bpctx.Connection.State != ConnectionState.Closed) bpctx.Connection.Close(); }
            return respReturn;
        }

    }
}